package cn.sjlink.util.notic;

import cn.sjlink.util.ConfigUtil;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dyvmsapi.model.v20170525.SingleCallByTtsRequest;
import com.aliyuncs.dyvmsapi.model.v20170525.SingleCallByTtsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

public class NoticeUtil {

    //产品名称
    private static final String product = "Dyvmsapi";
    //产品域名
    private static final String domain = "dyvmsapi.aliyuncs.com";

    private static final String accessKeyId = ConfigUtil.get("smsAccessKey");
    private static final String accessKeySecret = ConfigUtil.get("smsAccessSecret");

    /**
     * 文本转语音外呼
     * @return
     * @throws ClientException
     */
    public static SingleCallByTtsResponse singleCallByTts(String callNumber) throws ClientException {

        //可自助调整超时时间
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");

        //初始化acsClient,暂不支持region化
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
        IAcsClient acsClient = new DefaultAcsClient(profile);

        //组装请求对象-具体描述见控制台-文档部分内容
        SingleCallByTtsRequest request = new SingleCallByTtsRequest();
        //必填-被叫号码
        request.setCalledNumber(callNumber);
        //必填-Tts模板ID
        request.setTtsCode("TTS_215116677");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("orderName","线上订单");
        request.setTtsParam(jsonObject.toString());
        //hint 此处可能会抛出异常，注意catch
        SingleCallByTtsResponse singleCallByTtsResponse = acsClient.getAcsResponse(request);

        return singleCallByTtsResponse;
    }

}
